package tests.cases {
	
	import mx.rpc.AsyncToken;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	import org.flexunit.Assert;
	
	import vo.ForumPostVO;
	
	public class TestForum extends DefaultTestCase {
		
		private var forumService:RemoteObject;
		
		[Before]
		public function setUp():void {
			
			forumService = new RemoteObject();
			forumService.destination = "amfphp";
			forumService.source = "ForumService";
		}
		
		[After]
		public function tearDown():void {
			
			forumService = null;
		}
		
		// ----------------------------------------------------------------
		// TEST SAVE POST
		// ----------------------------------------------------------------
		private static var savedPostUID:String;
		
		[Test(async,order=1)]
		public function testSavePost():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001",
				userUID: "user001",
				heading: "Test Post",
				message: "This is a test forum post"
			};
			
			var token:AsyncToken = forumService.savePost(argsVO.sessionID, argsVO.groupUID, argsVO.userUID, argsVO.heading, argsVO.message);
			runAsyncTest(token, argsVO, handleSavePost);
		}
		
		private function handleSavePost(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			savedPostUID = message.result as String;
		}
		
		// ----------------------------------------------------------------
		// TEST SAVE COMMENT
		// ----------------------------------------------------------------
		private static var savedCommentUID:String;
		
		[Test(async,order=2)]
		public function testSaveComment():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				forumPostUID: savedPostUID,
				userUID: "user001",
				message: "This is a test comment"
			};
			
			var token:AsyncToken = forumService.saveComment(argsVO.sessionID, argsVO.forumPostUID, argsVO.userUID, argsVO.message);
			runAsyncTest(token, argsVO, handleSaveComment);
		}
		
		private function handleSaveComment(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			savedCommentUID = message.result as String;
		}
		
		// ----------------------------------------------------------------
		// TEST GET POST LIST
		// ----------------------------------------------------------------
		[Test(async,order=3)]
		public function testGetPostList():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				groupUID: "group001"
			};
			
			var token:AsyncToken = forumService.getPostList(argsVO.sessionID, argsVO.groupUID);
			runAsyncTest(token, argsVO, handleGetPostList);
		}
		
		private function handleGetPostList(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result.length > 0));
		}
		
		// ----------------------------------------------------------------
		// TEST GET POST DETAILS
		// ----------------------------------------------------------------
		[Test(async,order=4)]
		public function testGetPostDetails():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				forumPostUID: savedPostUID
			};
			
			var token:AsyncToken = forumService.getPostDetails(argsVO.sessionID, argsVO.forumPostUID);
			runAsyncTest(token, argsVO, handleGetPostDetails);
		}
		
		private function handleGetPostDetails(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNotNull(message.result);
			Assert.assertTrue((message.result is ForumPostVO));
		}
		
		// ----------------------------------------------------------------
		// TEST CENSOR COMMENT
		// ----------------------------------------------------------------
		[Test(async,order=5)]
		public function testCensorComment():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				forumCommentUID: savedCommentUID
			};
			
			var token:AsyncToken = forumService.censorComment(argsVO.sessionID, argsVO.forumCommentUID);
			runAsyncTest(token, argsVO, handleCensorComment);
		}
		
		private function handleCensorComment(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
		
		// ----------------------------------------------------------------
		// TEST CENSOR POST
		// ----------------------------------------------------------------
		[Test(async,order=6)]
		public function testCensorPost():void {
			
			var argsVO:Object = {
				sessionID: "debug",
				forumPostUID: savedPostUID
			};
			
			var token:AsyncToken = forumService.censorPost(argsVO.sessionID, argsVO.forumPostUID);
			runAsyncTest(token, argsVO, handleCensorPost);
		}
		
		private function handleCensorPost(message:ResultEvent, passedData:Object):void {
			
			Assert.assertNull(message.result);
		}
	}
}